package rexsee.core.utilities;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;

public class RexseeArray implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Array";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	private static final String DEFAULT_ID = "default";

	private final Browser mBrowser;
	private final HashMap<String, ArrayList<String>> mArray;

	public RexseeArray(Browser browser) {
		mBrowser = browser;
		mArray = new HashMap<String, ArrayList<String>>();
	}

	public int sizeIds() {
		return mArray.size();
	}
	public boolean containsId(String id) {
		return mArray.containsKey(id);
	}
	public void removeId(String id) {
		mArray.remove(id);
	}
	public void clearIds() {
		mArray.clear();
	}

	public void add(String value) {
		add(DEFAULT_ID, value);
	}
	public void add(String id, String value) {
		if (mArray.containsKey(id)) {
			mArray.get(id).add(value);
		} else {
			ArrayList<String> list = new ArrayList<String>();
			list.add(value);
			mArray.put(id, list);
		}
	}
	public String get(int index) {
		return get(DEFAULT_ID, index);
	}
	public String get(String id, int index) {
		if (mArray.containsKey(id)) {
			String rtn = mArray.get(id).get(index);
			return (rtn == null) ? "" : rtn;
		} else {
			return "";
		}
	}
	public String getJson() {
		return getJson(DEFAULT_ID);
	}
	public String getJson(String id) {
		if (mArray.containsKey(id)) {
			return Json.toJson(mArray.get(id));
		} else {
			return "[]";
		}
	}
	public void remove(int index) {
		remove(DEFAULT_ID, index);
	}
	public void remove(String id, int index) {
		if (mArray.containsKey(id)) {
			mArray.get(id).remove(index);
		}
	}
	public int size() {
		return size(DEFAULT_ID);
	}
	public int size(String id) {
		if (mArray.containsKey(id)) {
			return mArray.get(id).size();
		} else {
			return -1;
		}
	}

	public void explode(String str, String split) {
		explode(DEFAULT_ID, str, split);
	}
	public void explode(String id, String str, String split) {
		removeId(id);
		String[] arr = str.split(split);
		for (int i = 0; i < arr.length; i++) {
			add(id, arr[i]);
		}
	}
	public String implode(String split) {
		return implode(DEFAULT_ID, split);
	}
	public String implode(String id, String split) {
		if (mArray.containsKey(id)) {
			ArrayList<String> list = mArray.get(id);
			String rtn = "";
			for (int i = 0; i < list.size(); i++) {
				if (!rtn.equals("")) rtn += split;
				rtn += list.get(i);
			}
			return rtn;
		} else {
			return null;
		}
	}

	public void sort() {
		sort(DEFAULT_ID);
	}
	public void sort(String id) {
		if (mArray.containsKey(id)) {
			try {
				ArrayList<String> oList = mArray.get(id);
				String[] array = new String[oList.size()];
				for (int i = 0; i < array.length; i++) {
					array[i] = oList.get(i);
				}
				Arrays.sort(array, Collator.getInstance(Locale.CHINA));
				ArrayList<String> list = new ArrayList<String>();
				for (int i = 0; i < array.length; i++) {
					list.add(array[i]);
				}
				mArray.remove(id);
				mArray.put(id, list);
			} catch (Exception e) {
				mBrowser.exception(getInterfaceName(), e);
			}
		}
	}

}
